package cn.danger.com.model.action;

import android.content.Context;

import java.io.File;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import cn.danger.com.model.entity.MyUser;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.util.MyNetManager;

/**
 * Created by 俊 on 2016/12/28.
 */
public class UserInfoModelImpl implements UserInfoModel {

    private Context context;

    public UserInfoModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void uploadPhoto(File photo, final OnCommonListener listener) {
        if (photo != null) {
            if (new MyNetManager(context).isNetworkAvailable()) {
                final BmobFile bmobFile = new BmobFile(photo);
                bmobFile.uploadblock(new UploadFileListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            listener.onSuccess(bmobFile.getFileUrl());
                        } else {
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_UPDATE_PHOTO);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            } else {
                listener.onError(Constants.FAIL_TO_UPDATE_PHOTO);
            }
        } else {
            listener.onError(Constants.PHOTO_IS_EMPTY);
        }
    }

    @Override
    public void updatePhoto(String url, final OnCommonListener listener) {
        if (new MyNetManager(context).isNetworkAvailable()) {
            MyUser new_user = new MyUser();
            new_user.setPhoto(url);
            MyUser user = BmobUser.getCurrentUser(MyUser.class);
            if (user != null) {
                new_user.update(user.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            listener.onSuccess(null);
                        } else {
                            e.printStackTrace();
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_UPDATE_PHOTO);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            } else {
                listener.onError(Constants.LOGIN_IS_VALID);
            }
        } else {
            listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
        }
    }
}
